<template>
  <!-- 轮播图的最外侧容器，注意：如果一屏都没有的话，最外侧容易不要渲染，否则会有：默认最后一屏的bug -->
  <Swiper class="swiper" :options="swiperOption">
    <!-- 每个SwiperSlide对应轮播图的一屏 -->
    <SwiperSlide class="item" v-for="(s, index) in skuImageList" :key="s.id" @click.native="clickHnadler(index)">
      <img :src="s.imgUrl" alt="图片">
    </SwiperSlide>
    <!-- 左箭头（上一张） -->
    <div class="swiper-button-prev" slot="button-prev"></div>
    <!-- 右箭头（下一张） -->
    <div class="swiper-button-next" slot="button-next"></div>
  </Swiper>
</template>

<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'
export default {
  name: "ImageList",
  props: ['skuImageList'],
  components: { Swiper, SwiperSlide },
  data() {
    return {
      swiperOption: {
        slidesPerView: 3, // 同时展示几屏
        slidesPerGroup : 2, // 点击下一页,一次切几个slide
        // 左箭头与右箭头
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        },
      }
    }
  },
  methods: {
    // SwiperSlide 是一个组件,组件绑定click事件是自定义事件,自定义事件需要组件内部使用$emit触发
    // 如是让组件的自定义事件变成原生事件?
    // .native 修饰符,可以让组件的自定义事件变成原生的事件
    clickHnadler(index) {
      this.$bus.$emit('changeImg', index);
    }
  }
}
</script>

<style lang="less" scoped>
  .swiper-container {
    height: 56px;
    width: 412px;
    box-sizing: border-box;
    padding: 0 12px;

    .swiper-slide {
      width: 56px;
      height: 56px;

      img {
        width: 100%;
        height: 100%;
        border: 1px solid #ccc;
        padding: 2px;
        width: 50px;
        height: 50px;
        display: block;

        &.active {
          border: 2px solid #f60;
          padding: 1px;
        }

        &:hover {
          border: 2px solid #f60;
          padding: 1px;
        }
      }
    }

    .swiper-button-next {
      left: auto;
      right: 0;
    }

    .swiper-button-prev {
      left: 0;
      right: auto;
    }

    .swiper-button-next,
    .swiper-button-prev {
      box-sizing: border-box;
      width: 12px;
      height: 56px;
      background: rgb(235, 235, 235);
      border: 1px solid rgb(204, 204, 204);
      top: 0;
      margin-top: 0;
      &::after {
        font-size: 12px;
      }
    }
  }
</style>